package com.java.wmovie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.wmovie.entity.*;
import com.java.wmovie.mapper.LocationMapper;
import com.java.wmovie.mapper.MovieMapper;
import com.java.wmovie.mapper.OrdersMapper;
import com.java.wmovie.mapper.SliceMapper;
import com.java.wmovie.service.LocationService;
import com.java.wmovie.service.MovieService;
import com.java.wmovie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhw
 * @since 2022-01-27
 */
@Controller
public class MovieController {
    @Autowired
    MovieService movieService;
    @Autowired
    MovieMapper movieMapper;
    @Autowired
    LocationService locationService;
    @Autowired
    LocationMapper locationMapper;
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    SliceMapper sliceMapper;

    @RequestMapping("/movie")
    public String movie(Model model, HttpSession session, @RequestParam(value = "i",defaultValue = "1")Integer i){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        PageUtils pageUtils = new PageUtils(new Page(i, 4), "/movie");
        session.setAttribute("types","");
        session.setAttribute("region","");
        session.setAttribute("onlin","");
        Page<Movie> page = movieMapper.selectPage(pageUtils.getPage(),null);
        model.addAttribute("page",page);
        return "/movie";
    }

    @RequestMapping("/movie/by")
    public String MovieBy(HttpSession session,Model model,@RequestParam("name")String name){
        String name1= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name1);
        PageUtils pageUtils=new PageUtils(new Page(1,3),"/movie");
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        Page<Movie> page=movieMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/movie";
    }

    @RequestMapping("/movie/sy")
    public String MovieSy(HttpSession session,Model model,String types, String region, String onlin){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        PageUtils pageUtils=new PageUtils(new Page(1,17),"/movie");
        if(types!=null){
            session.setAttribute("types",types);
        }
        if (region!=null){
            session.setAttribute("region",region);
        }
        if (onlin!=null){
            session.setAttribute("onlin",onlin);
        }
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
            wrapper.like("types", session.getAttribute("types"));
            wrapper.like("region",session.getAttribute("region"));
            wrapper.like("onlin", session.getAttribute("onlin"));
        Page<Movie> page=movieMapper.selectPage(pageUtils.getPage(),wrapper);
        System.out.println(page.getRecords());
        model.addAttribute("page",page);
        return "/movie";
    }

    @RequestMapping("/movie/detailed")
    public String detailed(@RequestParam("id")Integer id,Model model,HttpSession session){
        QueryWrapper<Slice> wrapper=new QueryWrapper<>();
        wrapper.like("m_name",movieService.getById(id).getName());
        List<Slice> slices=sliceMapper.selectList(wrapper);
        model.addAttribute("slices",slices);
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        session.setAttribute("umId",id);
        Movie movie=movieService.getById(id);
        model.addAttribute("movie",movie);
        return "/detailed";
    }

    @RequestMapping("/movie/buy")
    public String MovieBuy(@RequestParam("id")Integer id,Model model,HttpSession session){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);

        session.setAttribute("idid",id);
        Movie movie=movieService.getById(id);
        session.setAttribute("mlname",movieService.getById(id).getName());
        session.setAttribute("price",movieService.getById(id).getPrice());
        model.addAttribute("movie",movie);
        QueryWrapper<Slice> wrapper=new QueryWrapper<>();
        wrapper.like("m_name",movieService.getById(id).getName());
        List<Slice> slices=sliceMapper.selectList(wrapper);
        model.addAttribute("slices",slices);
        return "/buy1";
    }

    @RequestMapping("/movie/buy1")
    public String MovieByu1(Model model,HttpSession session,@RequestParam("times")String times){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        session.setAttribute("times",times);
        Integer id = (Integer) session.getAttribute("idid");
        Movie movie=movieService.getById(id);
        model.addAttribute("movie",movie);
        return "/buy2";
    }

    @RequestMapping("/movie/buys")
    public String MovieBuys(Orders orders,@RequestParam("seat")String seat,@RequestParam("quantity")Integer quantity
    ,Model model,HttpSession session){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);

        String times= (String) session.getAttribute("times");
        String mName= (String) session.getAttribute("mlname");
        String uName= (String) session.getAttribute("loginUser");
        BigDecimal jg= (BigDecimal) session.getAttribute("price");
        BigDecimal sl = new BigDecimal(quantity);
        BigDecimal amount=sl.multiply(jg);
        Orders orders1=ordersMapper.selectOne(new QueryWrapper<Orders>().eq("seat",seat)
                .eq("times",times).eq("quantity",quantity).eq("amount",amount)
                .eq("u_name",uName).eq("m_name",mName));
        System.out.println(orders1);
        orders.setTimes(times);
        orders.setAmount(amount);
        orders.setMName(mName);
        orders.setUName(uName);
        ordersService.save(orders);

        return "redirect:/movie/buy2";
    }

    @RequestMapping("/movie/buy2")
    public String MovieBuy2(Model model,HttpSession session){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        Integer id = (Integer) session.getAttribute("idid");
        Movie movie=movieService.getById(id);
        model.addAttribute("movie",movie);
        return "/buy3";
    }




}

